import uuid
import time
import os

class QKDKey:
    def __init__(self, key_value, key_id=None):
        if key_id is None:
            self.key_id = uuid.uuid4().int & ((1 << 32) - 1)  # Generate a unique 32-bit value using UUID
        else:
            self.key_id = key_id  # Use provided key ID
        self.key_value = key_value
        self.key_size = len(key_value) * 8  # Calculate key size in bits
        self.generation_timestamp = time.time()  # Timestamp in seconds since the epoch

    @classmethod
    def generate_random_key(cls, key_size):
        byte_length = key_size // 8
        key_value = os.urandom(byte_length)  # Generate random bytes
        return cls(key_value)

    def __str__(self):
        return (f"Key ID: {self.key_id}\n"
                f"Key Size: {self.key_size} bits\n"
                f"Key Value: {self.key_value.hex()}\n"
                f"Generation Timestamp: {time.ctime(self.generation_timestamp)}")

if __name__ == "__main__":
    key = QKDKey(b'SampleKeyValue')
    print(key)

    random_key = QKDKey.generate_random_key(256)
    print(random_key)

    key_with_id = QKDKey(b'AnotherSampleKeyValue', key_id=12345678)
    print(key_with_id)
